package com.example.kotlinExample.repository.db;

import com.example.kotlinExample.entity.User;
import com.example.kotlinExample.vo.UserVo;
import org.hibernate.annotations.Cache;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

/**
 * @author liliang
 */
@CacheConfig(cacheNames = {""})
public interface UserRepository extends JpaRepository<User, Integer> {

    @Query("select u from User u where u.name like concat('%',:name, '%') ")
    Page<User> search(@Param("name") String keyword, Pageable pageable);

    @Query("select u.id,u.name,u.age,u.createTime,ui.gender,ui.nickname" +
            " from User u left join UserInfo ui on u.id = ui.userId where u.name like concat('%',:name, '%') or ui.nickname like concat('%',:name, '%')")
    Page<UserVo> searchUserInfo(@Param("name") String keyword, Pageable pageable);
}
